import { ref } from 'vue'
import type { TreeNodeData } from '@arco-design/web-vue'
import { OrgApi } from '@/apis'

/** 部门模块 */
export function useOrg(options?: { onSuccess?: () => void }) {
  const loading = ref(false)
  const orgTree = ref<TreeNodeData[]>([])

  const getOrgTree = async (name?: string) => {
    try {
      loading.value = true
      orgTree.value = await OrgApi.getTree({ name })
      options?.onSuccess?.()
    } finally {
      loading.value = false
    }
  }
  return { orgTree, getOrgTree, loading }
}

/** 校区模块 */
export function useCampus(options?: { immediate: boolean, onSuccess?: () => void }) {
  const loading = ref(false)
  const campusList = ref<TreeNodeData[]>([])

  const getCampusList = async (name?: string) => {
    try {
      loading.value = true
      campusList.value = await OrgApi.getList({ orgType: ['CAMPUS'], name })
      options?.onSuccess?.()
    } finally {
      loading.value = false
    }
  }
  if (options?.immediate) {
    getCampusList()
  }
  return { campusList, getCampusList, loading }
}
